package cn.sumpay.servlet;


import cn.sumpay.model.Message;
import cn.sumpay.model.User;
import cn.sumpay.model.VoteUserVoteMessage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static cn.sumpay.model.MessageType.*;
import static cn.sumpay.utils.DataSourceUtil.*;

/**
 * Created by Administrator on 2017/1/3.
 */
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        /*username = "zhuqianli";
        password = "zql";*/
        String verifycode = request.getParameter("verifycode");
        String role = request.getParameter("role");

        System.out.println(username+" "+password);

        HttpSession session = request.getSession();
        //验证码
        if(false)
        if(!verifycode.equalsIgnoreCase((String) session.getAttribute("verifycode"))){
            System.out.println(verifycode + "验证码错误" + session.getAttribute("verifycode"));
            session.setAttribute("message", "验证码错误");
            response.sendRedirect(request.getContextPath());
            return;
        }



        //账号密码验证
        try {
            Connection connection = getConnection();
            //获取当前活动id
            String active_id = "无";
            PreparedStatement p = connection.prepareStatement("select * from VOTE_ACTIVE where active_state=1");
            ResultSet r = p.executeQuery();
            if(r.next()){
                active_id = r.getString("active_id");
                String active_name = r.getString("active_name");
                request.getSession().setAttribute("active_id",active_id);
                request.getSession().setAttribute("active_name",active_name);
            }else{
                System.out.println("投票活动未开启或已关闭");
                request.setAttribute("message",new Message(info, "投票活动未开启或已关闭"));
                request.getRequestDispatcher("/WEB-INF/jsps/message.jsp").forward(request,response);
                return;
            }


            //判断是用户还是管理员
            if(role.equalsIgnoreCase("user")){
                String sql = "select * from vote_user where user_name=? and user_password = md5(?)";
                PreparedStatement pst = connection.prepareStatement(sql);
                pst.setString(1, username);
                pst.setString(2, password);
                ResultSet rs = pst.executeQuery();
                if(rs.next()){
                    System.out.println("用户登录成功");
                    User user = new User();
                    user.setUser_id(rs.getInt("user_id"));
                    user.setUser_name(rs.getString("user_name"));
                    user.setDepart_id(rs.getInt("depart_id"));
                    //判断用户是否投过票了
                    request.getSession().setAttribute("user",user);
                    PreparedStatement pst2 = connection.prepareStatement("select * from VOTE_USERVOTE_MESSAGE where user_id=? and active_id=?");
                    pst2.setInt(1,user.getUser_id());
                    pst2.setInt(2,Integer.valueOf(active_id));
                    ResultSet resultSet = pst2.executeQuery();
                    if(resultSet.next()){
                        System.out.println("已经投过票");
                        request.getSession().setAttribute("message", new Message(info,"已经投过票"));
                        List uservotemessagelist = new ArrayList();
                        //获取第一条
                        uservotemessagelist.add(new VoteUserVoteMessage(resultSet.getInt(1),resultSet.getInt(2),resultSet.getString(3),resultSet.getInt(4),resultSet.getString(5)));
                        while(resultSet.next()){
                            VoteUserVoteMessage vuvm = new VoteUserVoteMessage(resultSet.getInt(1),resultSet.getInt(2),resultSet.getString(3),resultSet.getInt(4),resultSet.getString(5));
                            uservotemessagelist.add(vuvm);
                        }
                        request.setAttribute("uservotemessagelist",uservotemessagelist);
                        request.getRequestDispatcher("/WEB-INF/jsps/index.jsp").forward(request,response);
                        return;
                    }
                    response.sendRedirect("user/index");
                    return;
                }else{
                    System.out.println("用户登录失败");
                    request.getSession().setAttribute("message", "账号或密码错误");
                    response.sendRedirect(request.getContextPath());
                    return;
                }
            }else{
                String sql = "select * from vote_admin where admin_name=? and admin_password=?";
                PreparedStatement pst = connection.prepareStatement(sql);
                pst.setString(1, username);
                pst.setString(2, password);
                ResultSet rs = pst.executeQuery();
                if(rs.next()){
                    System.out.println("管理员登录成功");
                    request.getSession().setAttribute("admin",username);
                    response.sendRedirect("admin/index");
                    return;
                }else{
                    System.out.println("管理员登录失败");
                    request.getSession().setAttribute("message", "账号或密码错误");
                    response.sendRedirect(request.getContextPath());
                    return;
                }
            }



        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
